package com.example.heathy_me.reportDetail.utils;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.heathy_me.R;

import java.util.ArrayList;
import java.util.List;

public class ReportTitleFragment extends Fragment {


    private reportDatabaseHelper dbHelper;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.report_title_frag, container, false);
        RecyclerView reportTitleRecyclerView = (RecyclerView) view.findViewById(R.id.report_title_recycler_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        reportTitleRecyclerView.setLayoutManager(layoutManager);
        ReportAdapter adapter = new ReportAdapter(getReport());
        reportTitleRecyclerView.setAdapter(adapter);
        String reportTitle = getActivity().getIntent().getStringExtra("report_title"); // 获取传入的标题
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    private List<Report> getReport() {


        dbHelper=new reportDatabaseHelper(getContext(),"Report.db",null,1);
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        List<Report> reportList = new ArrayList<>();
        Cursor cursor=db.query("report",null,null,null,null,null,null);
        if(cursor.moveToFirst()){
            do{
                Report report = new Report();
                String title=cursor.getString(cursor.getColumnIndex("title"));
                String content=cursor.getString(cursor.getColumnIndex("content"));
                report.setTitle(title);
                report.setContent(content);
                reportList.add(report);
            }while(cursor.moveToNext());
        }
        cursor.close();


//        for (int i = 1; i <= 50; i++) {
//            Report report = new Report();
//            report.setTitle("This is report title " + i);
//            report.setContent("This is report content " + i + ". ");
//            reportList.add(report);
//        }
        return reportList;
    }


    class ReportAdapter extends RecyclerView.Adapter<ReportAdapter.ViewHolder> {

        private List<Report> mReportList;

        class ViewHolder extends RecyclerView.ViewHolder {

            TextView reportTitleText;

            public ViewHolder(View view) {
                super(view);
                reportTitleText = (TextView) view.findViewById(R.id.report_title);
            }

        }

        public ReportAdapter(List<Report> reportList) {
            mReportList = reportList;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.report_item, parent, false);
            final ViewHolder holder = new ViewHolder(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Report report = mReportList.get(holder.getPosition());
                    ReportContentActivity.actionStart(getActivity(), report.getTitle(), report.getContent());

                }
            });
            return holder;
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            Report report = mReportList.get(position);
            holder.reportTitleText.setText(report.getTitle());
        }

        @Override
        public int getItemCount() {
            return mReportList.size();
        }

    }

}
